package com.itheima.health.service.impl;

import com.itheima.health.dao.*;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.dto.OrderVo;
import com.itheima.health.dto.SmsLoginParam;
import com.itheima.health.pojo.*;
import com.itheima.health.service.SendService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class SendServiceImpl implements SendService {
    @Autowired
    private SendDao sendDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckGroupItemDao checkGroupItemDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private SetmealDao setmealDao;
    @Override
    public void insert(SmsLoginParam smsLoginParam) {
        String telephone = smsLoginParam.getTelephone();
        Integer count=sendDao.isNot(telephone);
        if (count==0){
            sendDao.insert(smsLoginParam);
        }else {
            sendDao.update(smsLoginParam);
        }


    }

    @Override
    public Boolean check(SmsLoginParam smsLoginParam) {
        String code=sendDao.check(smsLoginParam.getTelephone());
        if (smsLoginParam.getValidateCode().equals(code)){
            return true;
        }
        return false;
    }

    @Override
    public List<Setmeal> select() {
        List<Setmeal> setmeals=sendDao.select();
        return setmeals;
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal=sendDao.findById(id);
        Integer[] CheckGroupIds=sendDao.findCheckGroupId(id);
        List<CheckGroup> checkGroups=new ArrayList<>();
        for (Integer checkGroupId : CheckGroupIds) {
            CheckGroup checkGroup = checkGroupDao.findById(checkGroupId);
            checkGroups.add(checkGroup);
        }

        for (CheckGroup checkGroup : checkGroups) {
            Integer checkGroupid = checkGroup.getId();
            Integer[] checkItems = checkGroupItemDao.find(checkGroupid);
            List<CheckItem> checkItems1 = new ArrayList<>();
            for (Integer checkItem : checkItems) {
                CheckItem checkItem1 = checkItemDao.selectById(checkItem);
                checkItems1.add(checkItem1);
            }
            checkGroup.setCheckItems(checkItems1);

        }
        setmeal.setCheckGroups(checkGroups);

        return setmeal;


    }

    @Override
    public void login(String telephone) {
        Integer count=sendDao.iN(telephone);
        Member member = new Member();
        member.setPhoneNumber(telephone);
        member.setRegTime(new Date());
        if (count==0){
            sendDao.into(member);
        }
    }

    @Override
    public Integer submit(OrderSubmitParam orderSubmitParam) {
        String telephone = orderSubmitParam.getTelephone();
        //检测验证是否正确

        String code=sendDao.check(telephone);

        if (!orderSubmitParam.getValidateCode().equals(code)){
            return 1;
        }
        //检测预约人数 是否满了
        Date orderDate = orderSubmitParam.getOrderDate();
        OrderSetting ordersetting = orderSettingDao.getOrderSettingByMonthAndDay(orderDate);
        if (ordersetting.getReservations()>=ordersetting.getNumber()){
            return 2;
        }
        //检测是否包含预约日期
        Integer countByDate = orderSettingDao.findCountByDate(orderDate);
        if (countByDate ==0){
            return 3;
        }
        //添加至会员信息
        String idCard = orderSubmitParam.getIdCard();//身份证
        String sex = orderSubmitParam.getSex();
        String name = orderSubmitParam.getName();
        String str = UUID.randomUUID().toString().replaceAll("-", "");
        Member member = new Member();
        member.setFileNumber(str.substring(0,19));
        member.setIdCard(idCard);
        member.setSex(sex);
        member.setName(name);
        member.setPhoneNumber(telephone);
        //查询电话是否被注册过 并补全会员信息
        Integer count=sendDao.iN(telephone);
        if (count==0){
            sendDao.into(member);
        }else {
            sendDao.edit(member);

        }
        //查询是否重复预约

        Integer memberId=sendDao.selectIdByPhone(telephone);
        Integer isNot=sendDao.checkRepeat(memberId,orderDate);
        if (isNot !=0){
            return 4;
        }
        //添加至订单表
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(orderSubmitParam.getSetMealId());
        sendDao.insertOrder(order);
        ordersetting.setReservations(ordersetting.getReservations()+1);
        orderSettingDao.edit(ordersetting);

        return 0;

    }

    @Override
    public OrderVo findOrderVoById(Integer id) {
        OrderVo orderVo = new OrderVo();
        Order order=sendDao.selectOrderById(id);
        Integer memberId = order.getMemberId();
        String name=sendDao.selectMemberById(memberId);

        Integer setmealId = order.getSetmealId();
        String setmealName=setmealDao.selectName(setmealId);
        orderVo.setMember(name);
        orderVo.setOrderDate(order.getOrderDate());
        orderVo.setSetmeal(setmealName);
        orderVo.setOrderType(order.getOrderType());

        return orderVo;
    }


}
